Skip to content

Conversation

matheusaaguiar
Copy link
Collaborator

part of #15727.

@matheusaaguiar matheusaaguiar self-assigned this Mar 15, 2025
@matheusaaguiar matheusaaguiar force-pushed the allowConstantsCustomStorageLayout branch 2 times, most recently from 944dc30 to 059349f Compare March 25, 2025 23:27
@matheusaaguiar matheusaaguiar force-pushed the allowConstantsCustomStorageLayout branch from 4aae044 to 00ee32e Compare March 27, 2025 02:03
"The base slot of the storage layout must evaluate to an integer."
);
return;
if (integerType->isSigned())
Copy link
Collaborator Author

@matheusaaguiar matheusaaguiar Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be removed from here and then replace the assert in line 173 as suggested in a previous PR #15528 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends. The range check should catch it anyway and it provides a message that will be clearer to the user so I'd rather not check it. Removing this and adding an assert would be a better choice.

But I'm not sure if we will actually reach that check with constants. What happens in this case if you remove the isSigned(). Will it fail in constant evaluator instead?

int constant X = -1;
contract C layout at X {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still fails the range check and the message is indeed clearer as you said:

Error: The base slot of the storage layout evaluates to -1, which is outside the range of type uint256.

@matheusaaguiar matheusaaguiar marked this pull request as ready for review March 27, 2025 02:20
@zaqk
Copy link

zaqk commented Apr 4, 2025

thank you for supporting constants! this is a huge composability unlock!

@zaqk
Copy link

zaqk commented Apr 4, 2025

Would this also apply to immutables?

@matheusaaguiar
Copy link
Collaborator Author

Would this also apply to immutables?

No, this only targets constants. Immutables are a little bit different and demand a different handling.

@cameel
Copy link
Collaborator

cameel commented Apr 11, 2025

We can't really support immutables here, because the layout must already be defined at creation time (constructor can initialize state variables), but their values can still change at that point. In case where they do not, the immutable can be replaced by a constant anyway.

@matheusaaguiar Speaking of immutables, please make sure you have a test case against #15989. I.e. one showing that immutables are rejected even if they end up being treated as pure by the compiler.

@matheusaaguiar matheusaaguiar force-pushed the allowConstantsCustomStorageLayout branch from ec9785d to d1dbd2e Compare April 22, 2025 19:47
6396_error,
baseSlotExpression.location(),
"The base slot of the storage layout must evaluate to a rational number."
"The base slot of the storage layout must evaluate to a rational integer number."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"The base slot of the storage layout must evaluate to a rational integer number."
"The base slot of the storage layout must evaluate to an integer."

We could also say a rational number or an integer, but not sure if the user will get the distinction. Unfortunately, out terminology here is a bit ambiguous.

contract C is A layout at A.x { }
// ----
// TypeError 6396: (68-71): The base slot of the storage layout must evaluate to a rational number.
// TypeError 1505: (68-71): The base slot expression cannot be evaluated during compilation.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is weird. Why are we getting this and not something like:

Error: Member "x" not found or not visible after argument-dependent lookup in type(contract A).

Is ConstantEvaluator silencing this error and just returning nullopt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, ConstantEvaluator can't handle member access. At the moment it can only process, literals, variables with no member acess from modules or other contracts and most of binary/unary operations.

Copy link
Collaborator

@cameel cameel May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. So apparently it's not just layouts, but ConstantEvaluator does not support such constants in general. It does not seem hard to add that though. Looking at ConstantEvaluator, it may be as simple as defining endVisit(MemberAccess) similar to endVisit(Identifier) it already has. I think we should do it: #16055.

This should not block this PR though. It's fine to leave it unsupported. You could add an explicit check for MemberAccess to PostTypeContractLevelChecker to issue a better message, but it will only catch cases where it's the top-level expression, so it's a half-measure.

import "A" as M;
contract C layout at M.x{ }
// ----
// TypeError 1505: (B:38-41): The base slot expression cannot be evaluated during compilation.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should work without errors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for constant_initialized_from_cast.sol

Comment on lines 2 to 3
address immutable a = 0x0000000000000000000000000000000000000001;
uint immutable x = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add an immutable that's not pure (i.e. one that's not initialized with a literal) since that's a separate case. Also a comment explaining how they differ.

contract C layout at ~uint(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) {}
// ----
// TypeError 6396: (21-94): The base slot of the storage layout must evaluate to a rational number.
// TypeError 1505: (21-94): The base slot expression cannot be evaluated during compilation.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that users will be confused about why we can't evaluate this, because fundamentally it should be possible. It's documented, but maybe it would not hurt to just say that the constant evaluator is just too limited for this?

Suggested change
// TypeError 1505: (21-94): The base slot expression cannot be evaluated during compilation.
// TypeError 1505: (21-94): The base slot expression contains elements that are not yet supported by the internal constant evaluator and therefore cannot be evaluated at compilation time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 1 to 3
contract at layout at type(uint).max { }
// ----
// TypeError 6396: (22-36): The base slot of the storage layout must evaluate to a rational number.
// TypeError 1505: (22-36): The base slot expression cannot be evaluated during compilation.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping this would work already in this PR, but apparently it's another thing that requires proper compile-time evaluation... #11183 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the constant evaluator doesn't seem to be evaluating much based on this PR :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, one thing that should work after the PR are simple constants initialized with expressions that we evaluate in unlimited precision. Please make sure that this is the case.

Copy link
Collaborator Author

@matheusaaguiar matheusaaguiar Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that's the same situation as the test cases in test/libsolidity/syntaxTests/storageLayoutSpecifier/intermediate_operation_out_of_range.sol, right?
I have added a few cases involving constants there.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. We should still have at least one dedicated test whose goal is specifically to show that constants work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're also missing tests with some expression on constants or with constants that refer to other constants. These should all work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is test/libsolidity/syntaxTests/storageLayoutSpecifier/constant_initialized_from_other_constant.sol and test/libsolidity/syntaxTests/storageLayoutSpecifier/layout_specification_constant_in_expression.sol which should cover that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. We should still have at least one dedicated test whose goal is specifically to show that constants work.

I separated the constant cases and put them in their own file test/libsolidity/syntaxTests/storageLayoutSpecifier/constant_initialized_with_unlimited_arithmetic_expression.sol.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label May 7, 2025
@matheusaaguiar matheusaaguiar removed the stale The issue/PR was marked as stale because it has been open for too long. label May 7, 2025
@matheusaaguiar matheusaaguiar force-pushed the allowConstantsCustomStorageLayout branch from d1dbd2e to 0d68b8c Compare May 15, 2025 15:20
@argotorg argotorg deleted a comment from github-actions bot May 16, 2025
@matheusaaguiar matheusaaguiar force-pushed the allowConstantsCustomStorageLayout branch from ced73ae to d19c5dc Compare May 21, 2025 12:36
@matheusaaguiar matheusaaguiar force-pushed the allowConstantsCustomStorageLayout branch from d19c5dc to c76f976 Compare June 4, 2025 13:55
Copy link

This pull request is stale because it has been open for 14 days with no activity.
It will be closed in 7 days unless the stale label is removed.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Jun 19, 2025
@cameel cameel removed the stale The issue/PR was marked as stale because it has been open for too long. label Jun 19, 2025
@nikola-matic nikola-matic self-requested a review June 23, 2025 13:36
Copy link

github-actions bot commented Jul 8, 2025

This pull request is stale because it has been open for 14 days with no activity.
It will be closed in 7 days unless the stale label is removed.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Jul 8, 2025
@matheusaaguiar matheusaaguiar removed the stale The issue/PR was marked as stale because it has been open for too long. label Jul 8, 2025
Copy link

This pull request is stale because it has been open for 14 days with no activity.
It will be closed in 7 days unless the stale label is removed.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Jul 23, 2025
@matheusaaguiar matheusaaguiar force-pushed the allowConstantsCustomStorageLayout branch from c76f976 to 64e46af Compare July 23, 2025 19:05
@github-actions github-actions bot removed the stale The issue/PR was marked as stale because it has been open for too long. label Jul 24, 2025
Copy link

github-actions bot commented Aug 7, 2025

This pull request is stale because it has been open for 14 days with no activity.
It will be closed in 7 days unless the stale label is removed.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Aug 7, 2025
@matheusaaguiar matheusaaguiar removed the stale The issue/PR was marked as stale because it has been open for too long. label Aug 7, 2025
Copy link

This pull request is stale because it has been open for 14 days with no activity.
It will be closed in 7 days unless the stale label is removed.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Aug 22, 2025
@matheusaaguiar matheusaaguiar removed the stale The issue/PR was marked as stale because it has been open for too long. label Aug 22, 2025
Copy link

github-actions bot commented Sep 6, 2025

This pull request is stale because it has been open for 14 days with no activity.
It will be closed in 7 days unless the stale label is removed.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Sep 6, 2025
@matheusaaguiar matheusaaguiar removed the stale The issue/PR was marked as stale because it has been open for too long. label Sep 7, 2025
6396_error,
baseSlotExpression.location(),
"The base slot of the storage layout must evaluate to a rational number."
"The base slot of the storage layout must evaluate to an integer number."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"The base slot of the storage layout must evaluate to an integer number."
"The base slot of the storage layout must evaluate to an integer."

An integer is a number, so this is a bit redundant. Also, couldn't you reuse error 1763? It's the same error and provides the same source location.

Copy link
Collaborator Author

@matheusaaguiar matheusaaguiar Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The concern I'd have with reusing error 1763 is that although it has the same location and message (which we could try to improve in both cases to differentiate them), it is a different case where the expression is a rational but it is fractional. In the case here of error 6396 the expression is neither a rational nor an integer number.
We can change the message here to The base slot of the storage layout must evaluate to a number. to clarify and distinguish the errors. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to cover both cases with the same error. The exact case is different (fractional literal vs non-integer constant), but conceptually the issue is the same with both - the user is trying to use something that's not an integer and we can't use that as a slot number.

Copy link
Collaborator

@cameel cameel Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, looking at the test cases, perhaps some clarification would be useful in cases where user is trying to use something like address or bytesNN since those can be initialized with an integer.

But that would be a completely different distinction. Meant to show that the type is the issue rather the value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we then change the message here to The base slot type must be a numerical type or something similar?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd keep it as "must evaluate to an integer" and just do a single check. It's hard to phrase these things.

What I'd really want to do eventually would be to rewrite the docs to introduce clear terminology.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a check to append a hint in the cases where the expression is of type address or bytesN.

@nikola-matic
Copy link
Collaborator

The inability of the constant evaluator to evaluate much at all aside, the PR looks good, so please go over the comments, and we can merge soon.

@matheusaaguiar matheusaaguiar force-pushed the allowConstantsCustomStorageLayout branch from 52a52f7 to 947b0df Compare September 9, 2025 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants